package com.android.lovingpet.community.category;

import android.app.Activity;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.android.lovingpet.R;
import com.android.lovingpet.base.BaseActivity;
import com.android.lovingpet.base.BasePresenter;
import com.android.lovingpet.base.BaseView;
import com.android.lovingpet.bean.PostEvent;
import com.android.lovingpet.community.post.PostActivity;

import org.greenrobot.eventbus.EventBus;

public class CategoryActivity extends BaseActivity {

    private static int REQUEST = 1;
    private TabLayout tabLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);
        ((TextView) findViewById(R.id.tv_title)).setText("分类");
        ViewPager viewPager = findViewById(R.id.vp_moment);
        viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
        tabLayout = findViewById(R.id.tl_moment);
        tabLayout.setupWithViewPager(viewPager);
        findViewById(R.id.fab_post).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CategoryActivity.this, PostActivity.class);
                intent.putExtra("type", tabLayout.getSelectedTabPosition() + 1);
                startActivityForResult(intent, REQUEST);
            }
        });
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public BaseView createView() {
        return this;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST && resultCode == Activity.RESULT_OK) {
            EventBus.getDefault().post(new PostEvent(data.getIntExtra("type", 0)));
        }
    }
}
